<?php

namespace Modules\Shop\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Category;
use Modules\Shop\Http\Requests\CategoryRequest;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$cates=Category::getCategory ();
        return view('shop::category.index',compact('cates'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('shop::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(CategoryRequest $request)
    {
    	Category::create($request->all());
    	session()->flash('success','添加分类成功！');
    	return redirect ()->back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('shop::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('shop::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(CategoryRequest $request,$id)
    {
    	$cate=Category::find($id);
    	$cate->cname=$request->input('cname');
    	$cate->pid=$request->input('pid');
    	$cate->save();
    	session()->flash('success','修改成功！');
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	$cate=Category::find($id);
    	$child=Category::hasChildCategory($cate);
    	if($child){
			session()->flash('danger','该分类下有子分类不可删除');
			return redirect ()->route('category.index');
		}
    	Category::destroy ($id);
    	session()->flash('succcess','删除分类成功！');
    	return redirect ()->route('category.index');
    }
}
